package com.ruoyi.common.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap;
import java.util.Map;

public class XmlBean {
    public static Map<String, String> XmlToMap(HttpServletRequest httpServletRequest) throws Exception {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(httpServletRequest.getInputStream());
        NodeList book = document.getElementsByTagName("xml");
        for (int i = 0; i < book.getLength(); i++) {
            Node item = book.item(i);
            if (item.hasChildNodes()) {
                NodeList childNodes = item.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    Node item1 = childNodes.item(j);
                    if (item1.getNodeType() == Node.ELEMENT_NODE)
                        stringStringHashMap.put(item1.getNodeName(), item1.getFirstChild().getNodeValue());
                }
            }
        }
        return stringStringHashMap;
    }
}
